home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / sb-file-panel.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  15.7 KB  |  536 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *    Ben Goodger
  22.  */
  23.  
  24. /*
  25.   Script for the file properties window
  26. */
  27.  
  28. function FileProperties()
  29. {
  30.   var tree = document.getElementById('fileTree');
  31.  
  32.   if (tree.selectedItems.length >= 1) {
  33.   // don't bother showing properties on bookmark separators
  34.   var type = tree.selectedItems[0].getAttribute('type');
  35.     if (type != "http://home.netscape.com/NC-rdf#BookmarkSeparator") {
  36.     var props = window.open("chrome://communicator/content/bookmarks/bm-props.xul",
  37.                                 "BookmarkProperties", "chrome,menubar,resizable");
  38.     props.BookmarkURL = tree.selectedItems[0].getAttribute("id");
  39.   }
  40.   } else {
  41.     dump("nothing selected!\n");
  42.   }
  43. }
  44.  
  45. function OpenSearch(tabName)
  46. {
  47.   window.openDialog("resource:/res/samples/search.xul", "SearchWindow", "dialog=no,close,chrome,resizable", tabName);
  48. }
  49.  
  50. function OpenURL(event, node)
  51. {
  52.     // clear any single-click/edit timeouts
  53.     if (timerID != null)
  54.     {
  55.         gEditNode = null;
  56.         clearTimeout(timerID);
  57.         timerID = null;
  58.     }
  59.  
  60.     if (node.getAttribute('container') == "true")
  61.     {
  62.         return(false);
  63.     }
  64.  
  65.     var url = node.getAttribute('id');
  66.  
  67.     // Ignore "NC:" urls.
  68.     if (url.substring(0, 3) == "NC:")
  69.     {
  70.         return(false);
  71.     }
  72.  
  73.   try
  74.   {
  75.     // add support for IE favorites under Win32, and NetPositive URLs under BeOS
  76.     if (url.indexOf("file://") == 0)
  77.     {
  78.       var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  79.       if (rdf)   rdf = rdf.QueryInterface(Components.interfaces.nsIRDFService);
  80.       if (rdf)
  81.       {
  82.         var fileSys = rdf.GetDataSource("rdf:files");
  83.         if (fileSys)
  84.         {
  85.           var src = rdf.GetResource(url, true);
  86.           var prop = rdf.GetResource("http://home.netscape.com/NC-rdf#URL", true);
  87.           var target = fileSys.GetTarget(src, prop, true);
  88.           if (target) target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
  89.           if (target) target = target.Value;
  90.           if (target) url = target;
  91.  
  92.         }
  93.       }
  94.     }
  95.   }
  96.   catch(ex)
  97.   {
  98.   }
  99.  
  100.     if(top.isEditor != undefined) {
  101.         if(top.editorShell) {
  102.             var ext = getFileExtension(url);
  103.             // look at the extension of the file to see what should be done
  104.             // with it. Note that this is a typically windowsey approach. will
  105.             // rdf:files hold a reference to the actual file type? (Mac?)
  106.             switch(ext) {
  107.             // XXX Crude, but it will do for now.
  108.             case "gif":
  109.             case "jpeg":
  110.             case "jpg":
  111.             case "png":
  112.                 // just insert the image
  113.                 EditorAutoInsertImage(url);
  114.                 break;
  115.             case "htm":
  116.             case "html":
  117.             case "xul":
  118.                 toEditor(url);
  119.                 break;
  120.             case "js":
  121.                 EditorInsertJSFile(url);
  122.                 break;
  123.             case "css":
  124.                 EditorInsertCSSFile(url);
  125.                 break;
  126.             default:
  127.                 // load the file in the editor
  128.                 dump("Editor Message: Weirdo File Format\n");
  129.                 toEditor(url);
  130.                 break;
  131.             }
  132.         }
  133.     } else {
  134.         window.open(url,'bookmarks');
  135.     }
  136.  
  137.     dump("OpenURL(" + url + ")\n");
  138.  
  139.     return(true);
  140. }
  141.  
  142. // returns the extension of a specified file URL
  143. function getFileExtension(url)
  144. {
  145.     return url.substring(url.lastIndexOf(".")+1,url.length).toLowerCase();
  146. }
  147.  
  148. var htmlInput = null;
  149. var saveNode = null;
  150. var newValue = "";
  151. var timerID = null;
  152. var gEditNode = null;
  153.  
  154. function DoSingleClick(event, node)
  155. {
  156.   var type = node.parentNode.parentNode.getAttribute('type');
  157.   var selected = node.parentNode.parentNode.getAttribute('selected');
  158.  
  159.   if (gEditNode == node) {
  160.     // Only start an inline edit if it is the second consecutive click
  161.     // on the same node that is not already editing or a separator.
  162.     if (!htmlInput &&
  163.         type != "http://home.netscape.com/NC-rdf#BookmarkSeparator") {
  164.       // Edit node if we don't get a double-click in less than 1/2 second
  165.       timerID = setTimeout("OpenEditNode()", 500);
  166.     }
  167.   } else {
  168.     if (htmlInput) {
  169.       // Clicked during an edit
  170.       // Save the changes and move on
  171.       CloseEditNode(true);
  172.     }
  173.     gEditNode = node;
  174.   }
  175.   return false;
  176. }
  177.  
  178. function OpenEditNode()
  179. {
  180.     dump("OpenEditNode entered.\n");
  181.  
  182.     // clear any single-click/edit timeouts
  183.     if (timerID != null)
  184.     {
  185.         clearTimeout(timerID);
  186.         timerID = null;
  187.     }
  188.  
  189.     // XXX uncomment the following line to replace the whole input row we do this
  190.     // (and, therefore, only allow editing on the name column) until we can
  191.     // arbitrarily change the content model (bugs prevent this at the moment)
  192.     gEditNode = gEditNode.parentNode;
  193.  
  194.     var name = gEditNode.parentNode.getAttribute("Name");
  195.     dump("Single click on '" + name + "'\n");
  196.  
  197.     var theParent = gEditNode.parentNode;
  198.     dump("Parent node is a " + theParent.nodeName + "\n\n");
  199.  
  200.     saveNode = gEditNode;
  201.  
  202.     // unselect all nodes!
  203.     var tree = document.getElementById('fileTree');
  204.     tree.clearSelection();
  205.  
  206.     // XXX for now, just remove the child from the parent
  207.     // optimally, we'd like to not remove the child-parent relationship
  208.     // and instead just set a "display: none;" attribute on the child node
  209.  
  210. //    gEditNode.setAttribute("style", "display: none;");
  211. //    dump("gEditNode hidden.\n");
  212.     theParent.removeChild(gEditNode);
  213.     gEditNode = null;
  214.     dump("gEditNode removed.\n");
  215.  
  216.     // create the html:input node
  217.     htmlInput = document.createElementNS("http://www.w3.org/1999/xhtml", "html:input");
  218.     htmlInput.setAttribute("value", name);
  219.     htmlInput.setAttribute("onkeypress", "return EditNodeKeyPress(event)");
  220.  
  221.     theParent.appendChild(htmlInput);
  222.     dump("html:input node added.\n");
  223.  
  224.     htmlInput.focus();
  225.  
  226.     dump("OpenEditNode done.\n");
  227.     return(true);
  228. }
  229.  
  230. function CloseEditNode(saveChangeFlag)
  231. {
  232.     dump("CloseEditNode entered.\n");
  233.  
  234.     if (htmlInput)
  235.     {
  236.         if (saveChangeFlag)
  237.         {
  238.             newValue = htmlInput.value;
  239.         }
  240.         dump("  Got html input: "+newValue+" \n");
  241.  
  242.         var theParent = htmlInput.parentNode;
  243.         theParent.removeChild(htmlInput);
  244.         theParent.appendChild(saveNode);
  245.         dump("  child node appended.\n");
  246.  
  247.         if (saveNode && saveChangeFlag)
  248.         {
  249.             var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  250.             RDF = RDF.QueryInterface(Components.interfaces.nsIRDFService);
  251.             var Bookmarks = RDF.GetDataSource("rdf:bookmarks");
  252.             dump("Got bookmarks datasource.\n");
  253.  
  254.             // XXX once we support multi-column editing, get the property
  255.             // from the column in the content model
  256.             var propertyName = "http://home.netscape.com/NC-rdf#Name";
  257.             var propertyNode = RDF.GetResource(propertyName, true);
  258.             dump("  replacing value of property '" + propertyName + "'\n");
  259.  
  260.             // get the URI
  261.             var theNode = saveNode;
  262.             var bookmarkURL = "";
  263.             while(true)
  264.             {
  265.                 var tag = theNode.nodeName;
  266.                 if (tag == "treeitem")
  267.                 {
  268.                     bookmarkURL = theNode.getAttribute("id");
  269.                     break;
  270.                 }
  271.                 theNode = theNode.parentNode;
  272.             }
  273.             dump("  uri is '" + bookmarkURL + "'\n");
  274.  
  275.             if (bookmarkURL == "")    return(false);
  276.             var bookmarkNode = RDF.GetResource(bookmarkURL, true);
  277.  
  278.  
  279.             dump("  newValue = '" + newValue + "'\n");
  280.             newValue = (newValue != "") ? RDF.GetLiteral(newValue) : null;
  281.  
  282.             var oldValue = Bookmarks.GetTarget(bookmarkNode, propertyNode, true);
  283.             if (oldValue)
  284.             {
  285.                 oldValue = oldValue.QueryInterface(Components.interfaces.nsIRDFLiteral);
  286.                 dump("  oldValue = '" + oldValue + "'\n");
  287.             }
  288.  
  289.             if (oldValue != newValue)
  290.             {
  291.                 if (oldValue && !newValue)
  292.                 {
  293.                     Bookmarks.Unassert(bookmarkNode, propertyNode, oldValue);
  294.                     dump("  Unassert used.\n");
  295.                 }
  296.                 else if (!oldValue && newValue)
  297.                 {
  298.                     Bookmarks.Assert(bookmarkNode, propertyNode, newValue, true);
  299.                     dump("  Assert used.\n");
  300.                 }
  301.                 else if (oldValue && newValue)
  302.                 {
  303.                     Bookmarks.Change(bookmarkNode, propertyNode, oldValue, newValue);
  304.                     dump("  Change used.\n");
  305.                 }
  306.  
  307.                 dump("re-writing bookmarks.html\n");
  308.                 var remote = Bookmarks.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  309.                 remote.Flush();
  310.             }
  311.  
  312.             newValue = "";
  313.             saveNode = null;
  314.         }
  315.         else dump("saveNode was null?\n");
  316.         htmlInput = null;
  317.     }
  318.     else dump("htmlInput was null?\n");
  319.  
  320.     dump("CloseEditNode done.\n");
  321. }
  322.  
  323. function EditNodeKeyPress(event)
  324. {
  325.     if (event.keyCode == 27)
  326.     {
  327.         CloseEditNode(false);
  328.         return(false);
  329.     }
  330.     else if (event.keyCode == 13 || event.keyCode == 10)
  331.     {
  332.         CloseEditNode(true);
  333.         return(false);
  334.     }
  335.     return(true);
  336. }
  337.  
  338. function doSort(sortColName)
  339. {
  340.   var node = document.getElementById(sortColName);
  341.   // determine column resource to sort on
  342.   var sortResource = node.getAttribute('resource');
  343.   if (!node) return(false);
  344.  
  345.   var sortDirection="ascending";
  346.   var isSortActive = node.getAttribute('sortActive');
  347.   if (isSortActive == "true") {
  348.     var currentDirection = node.getAttribute('sortDirection');
  349.     if (currentDirection == "ascending")
  350.       sortDirection = "descending";
  351.     else if (currentDirection == "descending")
  352.       sortDirection = "natural";
  353.     else
  354.       sortDirection = "ascending";
  355.   }
  356.  
  357.   // get RDF Core service
  358.   var rdfCore = XPAppCoresManager.Find("RDFCore");
  359.   if (!rdfCore) {
  360.     rdfCore = new RDFCore();
  361.     if (!rdfCore) {
  362.       return(false);
  363.     }
  364.     rdfCore.Init("RDFCore");
  365. //    XPAppCoresManager.Add(rdfCore);
  366.   }
  367.   // sort!!!
  368.   rdfCore.doSort(node, sortResource, sortDirection);
  369.   return(false);
  370. }
  371.  
  372.  
  373. function fillContextMenu(name,node)
  374. {
  375.     if (!name)    return(false);
  376.     var popupNode = document.getElementById(name);
  377.     if (!popupNode)    return(false);
  378.  
  379.     var url = GetFileURL(node);
  380.     var ext = getFileExtension(url);
  381.  
  382.     // remove the menu node (which tosses all of its kids);
  383.     // do this in case any old command nodes are hanging around
  384.     var menuNode = popupNode.childNodes[0];
  385.     popupNode.removeChild(menuNode);
  386.  
  387.     // create a new menu node
  388.     menuNode = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menu");
  389.     popupNode.appendChild(menuNode);
  390.  
  391.     dump("mwa");
  392.     if(ext == "gif")
  393.     {
  394.         // note: deleted all the doContextCmd stuff from bookmarks.
  395.         menuItem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem");
  396.         menuItem.setAttribute("label","Insert Image");
  397.         // menuItem.setAttribute("onaction","AutoInsertImage(\'" + url + "\')");
  398.         parent.appendChild(menuItem);
  399.     }
  400.  
  401.     return(true);
  402. }
  403.  
  404. function isImageFile(parent,url)
  405. {
  406. }
  407.  
  408. function GetFileURL(node)
  409. {
  410.     var url = node.getAttribute('id');
  411.  
  412.     // Ignore "NC:" urls.
  413.     if (url.substring(0, 3) == "NC:")
  414.     {
  415.         return(false);
  416.     }
  417.  
  418.   try
  419.   {
  420.     // add support for IE favorites under Win32, and NetPositive URLs under BeOS
  421.     if (url.indexOf("file://") == 0)
  422.     {
  423.       var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  424.       if (rdf)   rdf = rdf.QueryInterface(Components.interfaces.nsIRDFService);
  425.       if (rdf)
  426.       {
  427.         var fileSys = rdf.GetDataSource("rdf:files");
  428.         if (fileSys)
  429.         {
  430.           var src = rdf.GetResource(url, true);
  431.           var prop = rdf.GetResource("http://home.netscape.com/NC-rdf#URL", true);
  432.           var target = fileSys.GetTarget(src, prop, true);
  433.           if (target) target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
  434.           if (target) target = target.Value;
  435.           if (target) url = target;
  436.  
  437.         }
  438.       }
  439.     }
  440.   }
  441.   catch(ex)
  442.   {
  443.   }
  444.     return url;
  445. }
  446.  
  447.  
  448.  
  449. function doContextCmd(cmdName)
  450. {
  451.   dump("doContextCmd start: cmd='" + cmdName + "'\n");
  452.  
  453.   var treeNode = document.getElementById("bookmarksTree");
  454.   if (!treeNode)    return(false);
  455.   var db = treeNode.database;
  456.   if (!db)    return(false);
  457.  
  458.   var compositeDB = db.QueryInterface(Components.interfaces.nsIRDFDataSource);
  459.   if (!compositeDB)    return(false);
  460.  
  461.   var isupports = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  462.   if (!isupports)    return(false);
  463.   var rdf = isupports.QueryInterface(Components.interfaces.nsIRDFService);
  464.   if (!rdf)    return(false);
  465.  
  466.   // need a resource for the command
  467.   var cmdResource = rdf.GetResource(cmdName);
  468.   if (!cmdResource)        return(false);
  469.   cmdResource = cmdResource.QueryInterface(Components.interfaces.nsIRDFResource);
  470.   if (!cmdResource)        return(false);
  471.  
  472.   var select_list = treeNode.selectedItems;
  473.   if (select_list.length < 1)    return(false);
  474.  
  475.   dump("# of Nodes selected: " + select_list.length + "\n\n");
  476.  
  477.   // set up selection nsISupportsArray
  478.   var selectionInstance = Components.classes["@mozilla.org/supports-array;1"].createInstance();
  479.   var selectionArray = selectionInstance.QueryInterface(Components.interfaces.nsISupportsArray);
  480.  
  481.   // set up arguments nsISupportsArray
  482.   var argumentsInstance = Components.classes["@mozilla.org/supports-array;1"].createInstance();
  483.   var argumentsArray = argumentsInstance.QueryInterface(Components.interfaces.nsISupportsArray);
  484.  
  485.   // get argument (parent)
  486.   var parentArc = rdf.GetResource("http://home.netscape.com/NC-rdf#parent");
  487.   if (!parentArc)        return(false);
  488.  
  489.   for (var nodeIndex=0; nodeIndex<select_list.length; nodeIndex++)
  490.   {
  491.     var node = select_list[nodeIndex];
  492.     if (!node)    break;
  493.     var uri = node.getAttribute("ref");
  494.     if ((uri) || (uri == ""))
  495.     {
  496.       uri = node.getAttribute("id");
  497.     }
  498.     if (!uri)    return(false);
  499.  
  500.     var rdfNode = rdf.GetResource(uri);
  501.     if (!rdfNode)    break;
  502.  
  503.     // add node into selection array
  504.     selectionArray.AppendElement(rdfNode);
  505.  
  506.     // get the parent's URI
  507.     var parentURI="";
  508.     var theParent = node;
  509.     while (theParent)
  510.     {
  511.       theParent = theParent.parentNode;
  512.  
  513.       parentURI = theParent.getAttribute("ref");
  514.       if ((!parentURI) || (parentURI == ""))
  515.       {
  516.         parentURI = theParent.getAttribute("id");
  517.       }
  518.       if (parentURI != "")  break;
  519.     }
  520.     if (parentURI == "")    return(false);
  521.  
  522.     var parentNode = rdf.GetResource(parentURI, true);
  523.     if (!parentNode)  return(false);
  524.  
  525.     // add parent arc and node into arguments array
  526.     argumentsArray.AppendElement(parentArc);
  527.     argumentsArray.AppendElement(parentNode);
  528.   }
  529.  
  530.   // do the command
  531.   compositeDB.DoCommand( selectionArray, cmdResource, argumentsArray );
  532.  
  533.   dump("doContextCmd ends.\n\n");
  534.   return(true);
  535. }
  536.